﻿using System;
using System.Web.UI.WebControls;
using MaLu.Common;
using MaLu.Data;
using System.Data;
using System.Text;
using Wicresoft.Framework.Organization;
using MaLu.Framework.Common;
using Malu.Entity;

namespace MaluJg.web.FarmerShare
{
    public partial class FSEdit : FSPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "农民股金添加";


            if (!IsPostBack)
            {
                //Department mainD = OrganizationHelper.GetDepartmentInfo(CurrentUser.MainDepartmentID);
                //string[] ids = mainD.IDPath.Split('/');
                //if (ids.Length >= 4)
                //    DepartmentID = new Guid(ids[3]);
                //else
                //    DepartmentID = new Guid(ids[ids.Length - 1]);

                Binddata();


            }

        }
        protected int RecordID
        {
            get
            {
                int gd = 0;
                if (Request["ID"] != null)
                {
                    int.TryParse(Request["ID"] ?? "0", out gd);

                }
                else
                {
                    if (ViewState["ID"] != null)
                        gd = (int)ViewState["ID"];


                }
                if (ViewState["ID"] == null)
                    ViewState["ID"] = gd;
                return gd;
            }
            set
            {
                ViewState["ID"] = value;
            }

        }
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            FaShare fs = ProviderManager.Factory.FaShare.Get(Provider, RecordID);

            bool has = true;
            if (fs == null)
            {
                has = false;
                fs = new FaShare();


            }

            fs.amount = int.Parse(Util.ReturnSqlValue(tbAmount.Text.Trim(), DbType.Int32));
            fs.familyname = tbName.Text.Trim();
            fs.fax = float.Parse(Util.ReturnSqlValue(tbFax.Text.Trim(), DbType.Double));
            fs.honglipercent = float.Parse(Util.ReturnSqlValue(tbHongli.Text.Trim(), DbType.Double));
            fs.jine = float.Parse(Util.ReturnSqlValue(tbJinE.Text.Trim(), DbType.Double));
            fs.jinepergu = float.Parse(Util.ReturnSqlValue(tbJinePer.Text.Trim(), DbType.Double));
            fs.place = tbAdd.Text.Trim();
            fs.realget = float.Parse(Util.ReturnSqlValue(tbRealGet.Text.Trim(), DbType.Double));
            fs.village = tbVillage.Text.Trim();
            fs.yingde = float.Parse(Util.ReturnSqlValue(tbYingDe.Text.Trim(), DbType.Double));
            fs.remark = tbRemark.Text.Trim();
            fs.year = int.Parse(tbYear.Text.Trim());

            if (has)
                ProviderManager.Factory.FaShare.Update(Provider, fs);
            else
                ProviderManager.Factory.FaShare.Insert(Provider, fs);

            string script = "<script language=javascript>window.alert('保存成功！');refreshParent();window.close();</script>";
           
            this.ClientScript.RegisterStartupScript(this.GetType(), "save", script);
        }

        private void Binddata()
        {
            if (RecordID != 0)
            {
                FaShare fs = ProviderManager.Factory.FaShare.Get(Provider, RecordID);
                if (fs != null)
                {
                    this.Title = "农民股金编辑";
                    lbtnSave.Text = "<span ><img src=\"../Images/Icon/Action/save.gif\" alt=\"保存\" /></span><span>保存</span>";
                    tbAdd.Text = fs.place;
                    tbAmount.Text = fs.amount.ToString();
                    tbFax.Text = fs.fax.ToString();
                    tbHongli.Text = fs.honglipercent.ToString();
                    tbJinE.Text = fs.jine.ToString();
                    tbJinePer.Text = fs.jinepergu.ToString();
                    tbName.Text = fs.familyname;
                    tbRealGet.Text = fs.realget.ToString();
                    tbRemark.Text = fs.remark;
                    tbVillage.Text = fs.village;
                    tbYear.Text = fs.year.ToString();
                    tbYingDe.Text = fs.yingde.ToString();
                }
            }
        }
    }
}